package com.csq.cms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csq.cms.mapper.ParttimeInfoMapper;
import com.csq.cms.pojo.SignInfo;
import com.csq.cms.service.SignupService;
import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.manager.mapper.SignupMapper;
import com.csq.manager.pojo.Signup;
import com.csq.manager.pojo.SignupExample;
import com.csq.manager.pojo.SignupExample.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SignupServiceImpl implements SignupService {

	@Autowired
	private SignupMapper signupMapper;

	@Autowired
	private ParttimeInfoMapper parttimeInfoMapper;

	@Override
	public ReturnResult addSignup(Signup signup) {

		SignupExample example = new SignupExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andParttimeIdEqualTo(signup.getParttimeId());
		createCriteria.andUserIdEqualTo(signup.getUserId());
		List<Signup> selectByExample = signupMapper.selectByExample(example);

		if (selectByExample.size() != 0) {
			return ReturnResult.build(StatusCode.INSERTFAILL, "已经报名");
		}

		// TODO Auto-generated method stub
		int insertSelective = signupMapper.insertSelective(signup);
		if (insertSelective == 0) {
			return ReturnResult.build(StatusCode.INSERTFAILL, "插入失败");
		}

		return ReturnResult.ok();
	}

	@Override
	public PageInfo<SignInfo> getSignInfoByAccountIdAndType(Integer accountId,
			Integer type, Integer page, Integer row) {
		if (page == null) {
			page = 1;
		}
		if (row == null) {
			row = 10;
		}
		// 分页设置
		PageHelper.startPage(page, row);
		// 执行查询
		List<SignInfo> list = parttimeInfoMapper.getSignInfoByAccountIdAndType(
				accountId, type);

		for (SignInfo signInfo : list) {
			System.out.println(signInfo);
		}

		// 取分页信息
		PageInfo<SignInfo> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数：" + pageInfo.getTotal());
		System.out.println("总页数：" + pageInfo.getPages());
		System.out.println(list.size());
		return pageInfo;
	}

	@Override
	public ReturnResult deleteSignup(Integer signupId) {

		int deleteSelective = signupMapper.deleteByPrimaryKey(signupId);
		if (deleteSelective == 0) {
			return ReturnResult.build(StatusCode.DELETEFAIL, "刪除失败");
		}

		return ReturnResult.ok();
	}

	@Override
	public PageInfo<SignInfo> getSignInfoByEnterpriseIdAndType(
			Integer enterprisetId, Integer type, Integer page, Integer row) {
		if (page == null) {
			page = 1;
		}
		if (row == null) {
			row = 10;
		}
		// 分页设置
		PageHelper.startPage(page, row);
		// 执行查询
		List<SignInfo> list = parttimeInfoMapper
				.getSignInfoByEnterpriseIdAndType(enterprisetId, type);

		for (SignInfo signInfo : list) {
			System.out.println(signInfo);
		}

		// 取分页信息
		PageInfo<SignInfo> pageInfo = new PageInfo<>(list);
		System.out.println("总记录数：" + pageInfo.getTotal());
		System.out.println("总页数：" + pageInfo.getPages());
		System.out.println(list.size());
		return pageInfo;
	}

}
